<?php
//$acs:当前用户下账户
require_once ROOT_PATH.'/Abstract/front_abstract.php';
class Action extends front_abstract
{
    function index()
    {
        $ids = array();
        $accounts = array();
        $data = array();
        if ($_SERVER['REQUEST_METHOD']== 'POST') {
            if ($user_name = _POST('user_name')) {
                $user_name = array_unique(array_filter(explode(',', $user_name)));
                foreach ($user_name as $key => $val) {
                    $info = $this->api->show($this->access_token, array('screen_name'=>$val));
                    if (!isset($info['error'])) {
                        $accounts[$info['id']] = $info;
                        $ids[] = $info['id'];
                    }
                }
            }

            if(!_POST('begin_time')){
                $_POST['begin_time'] = date('Y-m-d', strtotime("last Month"));
            }

            if(!_POST('end_time')){
                $_POST['end_time'] = date('Y-m-d');
            }

            $begin_time = strtotime(_POST('begin_time'));
            $end_time = strtotime(_POST('end_time'));

            $this->smarty->assign('begin_time', _POST('begin_time'));
            $this->smarty->assign('end_time', _POST('end_time'));
        }else{
            if($user_name = _model('user')->read(array('id'=>$_SESSION['user']['id']))){
                $user_name = array_unique(array_filter(explode(',', $user_name['accounts'])));
                array_unshift($user_name, $_SESSION['user']['account_id']);
                foreach ($user_name as $key => $val) {
                    $info = $this->api->show($this->access_token, array('user_id'=>$val));
                    if (!isset($info['error'])) {
                        $accounts[$info['id']] = $info;
                        $ids[] = $info['id'];
                    }
                }
            }
            $begin_time = strtotime("last Month");
            $end_time = time();

            $this->smarty->assign('begin_time', date('Y-m-d', $begin_time));
            $this->smarty->assign('end_time', date('Y-m-d'));
        }

        if ($ids) {
            foreach ($ids as $key => $val) {
                $info = _model('account_info')->read("WHERE account_id = $val AND check_time >= $begin_time ORDER BY check_time");
                if (!$info) {
                    continue;
                }
                $info2 = _model('account_info')->read("WHERE account_id = $val AND check_time <= $end_time ORDER BY check_time DESC");
                $data[] = array(
                    'begin' => $info,
                    'end' => $info2,
                    'name' => $accounts[$val]['screen_name']
                );
            }
            $this->smarty->assign('data', $data);
            // $this->smarty->assign('accounts', $accounts);
        }
        $this->smarty->assign('data', $data);
        $this->smarty->display('data.html');
    }
    
    function getdata($acs, $first, $last){
        $data = array();
        foreach($acs as $key => $val){
            $data[$val] = _model('account_info');
        }
    }
}
?>